<template>
	<div>
		<div v-if="this.attendanceName === ''">
			<div class="my-code">点击考勤查看详情</div>
		</div>
		<div v-else>
			<!--工具栏-->
			<div class="head-container">
				<div v-if="crud.props.searchToggle">
					<!-- 搜索 -->
					<el-input v-model="query.name" clearable size="small" placeholder="输入考勤标签查询" style="width: 200px;"
						class="filter-item" @keyup.enter.native="toQuery" />
					<rrOperation />
				</div>
			</div>
			<!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
			<!-- <crudOperation :permission="permission" /> -->
			<!--表单组件-->
			<el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
				:title="crud.status.title" width="500px">
				<el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
					<el-form-item label="所属考勤">
						<el-tag style="width: 370px;">{{ this.attendanceName }}</el-tag>
					</el-form-item>
					<el-form-item label="班级">
						<el-tag style="width: 370px;">{{ this.className }}</el-tag>
					</el-form-item>
					<el-form-item label="学生">
						<el-tag style="width: 370px;">{{ this.studentName }}</el-tag>
					</el-form-item>
					<el-form-item label="出勤情况" style="width: 370px;" >
						<el-select v-model="form.isCheckIn" placeholder="请选择">
							<el-option v-for="item in dict.check_in_status" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button type="text" @click="crud.cancelCU">取消</el-button>
					<el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
				</div>
			</el-dialog>
			<div style="margin-left: 5px; margin-right: 5px;">
				<el-row>
					<el-col style="width: 170px; padding: 4px;" v-for="(item) in crud.data" :key="item.attDetialId">
						<el-card shadow="hover" :style="item.isCheckIn=='ABSENT'?'background-color: #ff0000':
						item.isCheckIn=='LEAVE'?'background-color: #e7d12a':
						item.isCheckIn=='CHECKIN'?'background-color: #00aa00':''">
							<div>
								<span style="width: 170px; font-size: 20px;">{{item.student.name}}</span>
								<div style="margin-top: 10px;">
									<el-select v-model="item.isCheckIn" placeholder="请选择" @change="changeCheckInStatus(item, item.isCheckIn,)">
										<el-option v-for="itemStatus in dict.check_in_status" :key="itemStatus.value"
											:label="itemStatus.label" :value="itemStatus.value">
										</el-option>
									</el-select>
								</div>
							</div>
						</el-card>
					</el-col>
				</el-row>
			</div>
			
			<!--表格渲染-->
			<!-- <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
				@selection-change="crud.selectionChangeHandler">
				<el-table-column type="selection" width="55" />
				<el-table-column label="所属考勤">
					{{ query.attendanceName }}
				</el-table-column>
				<el-table-column label="班级" width="100">
					<template slot-scope="scope">
						{{ scope.row.student.classModel.name }}
					</template>
				</el-table-column>
				<el-table-column label="学生" width="100" >
					<template slot-scope="scope">
						{{ scope.row.student.name }}
					</template>
				</el-table-column>
				<el-table-column prop="isCheckIn" label="出勤情况" width="150" >
					<template slot-scope="scope">
						<el-select v-model="scope.row.isCheckIn" placeholder="请选择" @change="changeCheckInStatus(scope.row, scope.row.isCheckIn,)">
							<el-option v-for="item in dict.check_in_status" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-select>
					</template>
				</el-table-column>
				<el-table-column v-if="checkPer(['admin','attendanceDetail:edit','attendanceDetail:del'])" label="操作"
					width="150px" align="center">
					<template slot-scope="scope">
						<udOperation :data="scope.row" :permission="permission" />
					</template>
				</el-table-column>
			</el-table> --> <!-- -->
			<!--分页组件-->
			<!-- <pagination /> -->
		</div>
	</div>
</template>

<script>
	import crudAttendanceDetail from '@/api/attendanceDetail'
	import CRUD, {
		presenter,
		header,
		form,
		crud
	} from '@crud/crud'
	import rrOperation from '@crud/RR.operation'
	import udOperation from '@crud/UD.operation'
	import pagination from '@crud/Pagination'

	const defaultForm = {
		attDetialId: null,
		attendanceId: null,
		studentId: null,
		isCheckIn: null
	}
	export default {
		name: 'attendanceDetail',
		components: {
			pagination,
			// crudOperation,
			rrOperation,
			udOperation
		},
		mixins: [presenter(), header(),
			form(function() {
				return Object.assign({
					attendance: {
						id: this.attendanceId
					}
				}, defaultForm)
			}), crud()
		],
		dicts: ['check_in_status'],
		cruds() {
			return CRUD({
				title: '考勤明细',
				url: 'api/attendanceDetail',
				query: {
					attendanceId: ''
				},
				idField: 'attDetialId',
				sort: 'attDetialId,desc',
				crudMethod: {
					...crudAttendanceDetail
				},
				optShow: {
					add: true,
					edit: true,
					del: true,
					reset: false
				},
				queryOnPresenterCreated: false,
				page:{size: 100}
			})
		},
		data() {
			return {
				attendanceId: null,
				className: '',
				studentName: '',
				attendanceName: '',
				permission: {
					add: ['admin', 'attendanceDetail:add'],
					edit: ['admin', 'attendanceDetail:edit'],
					del: ['admin', 'attendanceDetail:del']
				},
				rules: {}
			}
		},
		methods: {
			// 钩子：在获取表格数据之前执行，false 则代表不获取数据
			[CRUD.HOOK.beforeRefresh]() {
				return true
			},
			[CRUD.HOOK.afterToCU](crud, form) {
				this.className = `${form.student.classModel.name}`
				this.studentName = `${form.student.name}`
			},
			changeCheckInStatus(data, val){
				console.log(val)
				crudAttendanceDetail.edit(data).then(res => {
					this.crud.notify(data.student.name + '的考勤信息修改为：[' + this.dict.label.check_in_status[val] + '] 成功', CRUD.NOTIFICATION_TYPE
						.SUCCESS)
				}).catch(err => {
					// data.enabled = !data.enabled
					console.log(err.response.data.message)
				})
			}
		}
	}
</script>

<style scoped>
	el-card{
		&.low{
			background-color: #808080;
		}
		&.in{
			background-color: #4ca0ff;
		}
		&.high{
			background-color: #fb894f;
		}
		&.urgent{
			background-color: #f95f62;
		}
	}
</style>
